package com.example.login.service;
import com.example.login.entity.User;
import com.example.login.mapper.LoginMapper;
import com.example.login.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class LoginService {


    @Autowired
    private LoginMapper loginMapper;

    public List<User> getAllLogin() {
        List<User> logins = loginMapper.getAllLogin();
        return logins;    }


    public boolean register(String userName, String name, String major, String className, String studentNum, String password) {
        // 检查用户名和学号是否已存在
        User existingUser = loginMapper.findByUsernameOrStudentId(userName, studentNum);
        if (existingUser != null) {
            return false;
        }

        // 创建新的Login对象并保存到数据库
        User newUser = new User();
        newUser.setUserName(userName);
        newUser.setName(name);
        newUser.setMajor(major);
        newUser.setClass_name(className);
        newUser.setStudentNum(studentNum);
        newUser.setPassword(password);
        loginMapper.save(newUser);
        return true;
    }

    public User checkLogin(String username, String password) {
        User user  = loginMapper.findByUsernameAndPassword(username, password);
        return user;
    }

}
